﻿using System;
using System.Web;
using System.Web.Security;

namespace Yala.Core.Authentication {
    public class AspNetFormsAuthentication : IFormsAuthentication {

        public void SignIn(Guid userID, string userName, bool createPersistentCookie) {
            if(string.IsNullOrEmpty(userName))
                throw new ArgumentException("Value cannot be null or empty.", "userName");
            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);

            string userData = userID.ToString();

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName,
                DateTime.Now, DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
                createPersistentCookie, userData);
            string hashedTicket = FormsAuthentication.Encrypt(ticket);

            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashedTicket);
            HttpContext.Current.Response.Cookies.Add(cookie);

        }

        public void SignOut() {
            FormsAuthentication.SignOut();
        }
    }
}
